package com.coffee.core.result;

import com.coffee.core.enums.ExceptionEnum;

/**
 * @author Xulg
 */
@SuppressWarnings({"WeakerAccess", "unused"})
public class Result<DATA> extends CommonResult {
    private static final long serialVersionUID = 7661808742203573392L;

    private static final String DEFAULT_MESSAGE = "SUCCESS";
    private static final Integer DEFAULT_CODE = 200000;
    private static final Integer ERR_CODE = 400;

    /**
     * 结果数据
     */
    private DATA data;

    /**
     * 成功结果
     *
     * @param code    the code
     * @param message the message
     * @param data    the data type
     * @return the result
     */
    public static <T> Result<T> success(T data, Integer code, String message) {
        Result<T> result = new Result<>();
        result.setCode(code);
        result.setData(data);
        result.setSuccess(true);
        result.setMsg(message);
        return result;
    }

    /**
     * 成功结果
     *
     * @param data the data type
     * @return the result
     */
    public static <T> Result<T> success(T data) {
        return Result.success(data, DEFAULT_CODE, DEFAULT_MESSAGE);
    }

    /**
     * 成功结果
     *
     * @param code the code
     * @param data the data
     * @return the result
     */
    public static <T> Result<T> success(T data, Integer code) {
        return Result.success(data, code, DEFAULT_MESSAGE);
    }

    /**
     * 失败结果
     *
     * @param code    the code
     * @param message the message
     * @return the fail result
     */
    public static <T> Result<T> failed(Integer code, String message) {
        Result<T> result = new Result<>();
        result.setData(null);
        result.setSuccess(false);
        result.setCode(code);
        result.setMsg(message);
        return result;
    }

    /**
     * 失败结果
     *
     * @param exceptionEnum 异常枚举
     * @return the fail result
     */
    public static <T> Result<T> failed(ExceptionEnum exceptionEnum) {
        Result<T> result = new Result<>();
        result.setData(null);
        result.setSuccess(false);
        try {
            result.setCode(exceptionEnum.getCode());
        } catch (Exception e) {
            result.setCode(ERR_CODE);
        }
        result.setMsg(exceptionEnum.getMessage());
        return result;
    }

    public DATA getData() {
        return data;
    }

    public void setData(DATA data) {
        this.data = data;
    }
}
